package org.bluedream.core.module.service.fixed;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.collections.map.HashedMap;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.comm.utils.RestResult;
import org.bluedream.core.module.base.OrgBaseServiceImpl;
import org.bluedream.core.module.dao.fixed.FixedDao;
import org.bluedream.core.module.entity.fixed.Fixed;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.Serializable;
import java.sql.Date;
import java.util.Map;

/**
 * @ClassName FixedService
 * @Description TODO
 * @Author Administrator
 * @Data 2021/11/29 16:29
 * @Version
 */
@Service
@DS("master")
@Transactional(
        readOnly = true
)
public class FixedService extends OrgBaseServiceImpl<FixedDao, Fixed> implements IService<Fixed> {
    private static final Logger logger = LoggerFactory.getLogger(FixedService.class);

    @Override
    public Fixed getById(Serializable id) {
        if (EmptyUtil.isEmpty(id)){
            return new Fixed();
        }
        return super.getById(id);
    }

    public IPage<Fixed> findPage(Fixed entity, String... args) {
        Map<String , Object> search = null;
        search = new HashedMap();
        if (EmptyUtil.isNoEmpty(entity.getSkuLocation())
                || EmptyUtil.isNoEmpty(entity.getCardNum())
                || EmptyUtil.isNoEmpty(entity.getGeCheckDate())
                || EmptyUtil.isNoEmpty(entity.getLtCheckDate())
                || EmptyUtil.isNoEmpty(entity.getGeCreateDate())
                || EmptyUtil.isNoEmpty(entity.getLtCreateDate())
        ){
            try {
                if (EmptyUtil.isNoEmpty(entity.getGeCreateDate())){
                    search.put("create_date;ge" , Date.valueOf(entity.getGeCreateDate()));
                }
                if (EmptyUtil.isNoEmpty(entity.getLtCreateDate())){
                    search.put("create_date;lt" , Date.valueOf(entity.getLtCreateDate()));
                }
                if (EmptyUtil.isNoEmpty(entity.getGeCheckDate())){
                    search.put("check_date;ge" , Date.valueOf(entity.getGeCheckDate()));
                }
                if (EmptyUtil.isNoEmpty(entity.getLtCheckDate())){
                    search.put("check_date;lt" , Date.valueOf(entity.getLtCheckDate()));
                }
            }catch (Exception e){
                e.printStackTrace();
                logger.error(e.getMessage());
            }

            if (EmptyUtil.isNoEmpty(entity.getCardNum())){
                search.put("card_num" , entity.getCardNum());
            }
        }else {
            long limit = entity.getLimit();
            long page = entity.getPage();
            entity = new Fixed();
            entity.setLimit(limit);
            entity.setPage(page);
        }
        return super.findPage(entity , search , args);
    }

    @Transactional(
            readOnly = false,
            rollbackFor = Exception.class
    )
    public RestResult saveEntity(Fixed entity){
        try {
            super.save(entity);
        }catch (Exception e1){
            e1.printStackTrace();
            logger.error(e1.getMessage());
            //todo:@Transactional和try catch捕获异常会让注解失效
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RestResult.error("数据保存失败:" + e1.getMessage());
        }
        return RestResult.ok();
    }
}